<?php

namespace app\common\model\tick;

use think\Model;


class TickCode extends Model
{


    // 表名
    protected $table = 'tick_code';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'type_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }

    /*
     * 类型:1=抖音极速版推广码,2=快手极速版推广码,3=今日头条极速版推广,4=拼多多活动
     * */
    public function getTypeList()
    {
        return ['1' => __('抖音极速版'), '2' => __('快手极速版推广码'), '3' => __('今日头条极速版推广'), '4' => __('拼多多活动')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 子码关联
     * */
    public function codeSonRe()
    {
        return $this->hasMany(TickCodeSon::class)->order('weigh, id', 'asc');
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
